﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;

namespace InternshipManagement.Helpers
{
    public static class HtmlExtensions
    {
        #region WatermarkedTextBox
        public static MvcHtmlString WatermarkedTextBox(this HtmlHelper htmlHelper, string name, string watermark, object value, object htmlAttributes)
        {
            TagBuilder label = new TagBuilder("label");
            label.MergeAttribute("for", name);
            label.InnerHtml = watermark;
            string input = String.Format(@"<span class=""watermarkedInput"">{0}{1}</span>"
                                         , htmlHelper.TextBox(name, value, htmlAttributes)
                                         ,label.ToString(TagRenderMode.Normal));
            return MvcHtmlString .Create(input);
        }
        public static MvcHtmlString WatermarkedTextBox(this HtmlHelper htmlHelper, string name, string watermark, object value)
        {
            return htmlHelper.WatermarkedTextBox(name, watermark, value, null);
        }
        public static MvcHtmlString WatermarkedTextBox(this HtmlHelper htmlHelper, string name, string watermark)
        {
            return htmlHelper.WatermarkedTextBox(name, watermark, null);
        }

        public static MvcHtmlString WatermarkedTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark, object htmlAttributes) where TModel : class
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            return htmlHelper.WatermarkedTextBox(metadata.PropertyName, watermark, metadata.Model as string, htmlAttributes);
        }
        public static MvcHtmlString WatermarkedTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark) where TModel : class
        {
            return htmlHelper.WatermarkedTextBoxFor(expression, watermark, null);
        }
        #endregion

        #region WatermarkedPassword
        public static MvcHtmlString WatermarkedPassword(this HtmlHelper htmlHelper, string name, string watermark, object value, object htmlAttributes)
        {
            TagBuilder label = new TagBuilder("label");
            label.MergeAttribute("for", name);
            label.InnerHtml = watermark;
            string input = String.Format(@"<span class=""watermarkedInput"">{0}{1}</span>"
                                         , htmlHelper.Password(name, value, htmlAttributes)
                                         , label.ToString(TagRenderMode.Normal));
            return MvcHtmlString.Create(input);
        }
        public static MvcHtmlString WatermarkedPassword(this HtmlHelper htmlHelper, string name, string watermark, object value)
        {
            return htmlHelper.WatermarkedPassword(name, watermark, value, null);
        }
        public static MvcHtmlString WatermarkedPassword(this HtmlHelper htmlHelper, string name, string watermark)
        {
            return htmlHelper.WatermarkedPassword(name, watermark, null);
        }

        public static MvcHtmlString WatermarkedPasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark, object htmlAttributes) where TModel : class
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            return htmlHelper.WatermarkedPassword(metadata.PropertyName, watermark, metadata.Model as string, htmlAttributes);
        }
        public static MvcHtmlString WatermarkedPasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark) where TModel : class
        {
            return htmlHelper.WatermarkedPasswordFor(expression, watermark, null);
        }
        #endregion

        #region CustomCheckBox

        public static MvcHtmlString CustomCheckBox(this HtmlHelper htmlHelper, string name, bool? isChecked, object htmlAttributes)
        {
            TagBuilder span = new TagBuilder("span");
            span.GenerateId("span" + name);
            span.MergeAttribute("name", "span" + name);
            span.AddCssClass("customCheckbox");
            TagBuilder hidden = new TagBuilder("input");
            hidden.MergeAttribute("type", "hidden");
            hidden.MergeAttribute("name", name);
            hidden.MergeAttribute("value", isChecked == true ? "true" : "false");
            string input = span.ToString(TagRenderMode.Normal) + hidden.ToString(TagRenderMode.SelfClosing);
            return MvcHtmlString.Create(input);
        }
        public static MvcHtmlString CustomCheckBox(this HtmlHelper htmlHelper, string name, bool? isChecked)
        {
            return htmlHelper.CustomCheckBox(name, isChecked, null);
        }
        public static MvcHtmlString CustomCheckBox(this HtmlHelper htmlHelper, string name)
        {
            return htmlHelper.CustomCheckBox(name, null);
        }

        public static MvcHtmlString CustomCheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            return htmlHelper.CustomCheckBox(metadata.PropertyName, metadata.Model as bool?, htmlAttributes);
        }
        public static MvcHtmlString CustomCheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression)
        {
            return htmlHelper.CustomCheckBoxFor(expression, null);
        }

        #endregion

        #region WatermarkedTextArea
        public static MvcHtmlString WatermarkedTextArea(this HtmlHelper htmlHelper, string name, string watermark, object value, object htmlAttributes)
        {
            TagBuilder label = new TagBuilder("label");
            label.MergeAttribute("for", name);
            label.InnerHtml = watermark;
            string input = String.Format(@"<span class=""watermarkedInput"">{0}{1}</span>"
                                         , htmlHelper.TextArea(name, value == null ? "" : value.ToString(), htmlAttributes)
                                         , label.ToString(TagRenderMode.Normal));
            return MvcHtmlString.Create(input);
        }
        public static MvcHtmlString WatermarkedTextArea(this HtmlHelper htmlHelper, string name, string watermark, object value)
        {
            return htmlHelper.WatermarkedTextArea(name, watermark, value, null);
        }
        public static MvcHtmlString WatermarkedTextArea(this HtmlHelper htmlHelper, string name, string watermark)
        {
            return htmlHelper.WatermarkedTextArea(name, watermark, null);
        }

        public static MvcHtmlString WatermarkedTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark, object htmlAttributes) where TModel : class
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            return htmlHelper.WatermarkedTextArea(metadata.PropertyName, watermark, metadata.Model as string, htmlAttributes);
        }
        public static MvcHtmlString WatermarkedTextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string watermark) where TModel : class
        {
            return htmlHelper.WatermarkedTextAreaFor(expression, watermark, null);
        }
        #endregion

        #region CustomFileUpload
        public static MvcHtmlString CustomFileUpload(this HtmlHelper htmlHelper, string name,int size)
        {
            TagBuilder button = new TagBuilder("span");
            button.AddCssClass("customfilebutton");
            button.InnerHtml = "Browse...";
            TagBuilder file = new TagBuilder("input");
            file.GenerateId(name);
            file.AddCssClass("customfileinput");
            file.MergeAttribute("type","file");
            file.MergeAttribute("size", size.ToString());
            TagBuilder text = new TagBuilder("input");
            text.MergeAttribute("type", "text");
            text.AddCssClass("customfilefeedback");
            string input = String.Format(@"<div class=""customFileUploadDiv"">{0}{1}{2}</div>"
                                         , text.ToString(TagRenderMode.Normal)
                                         , button.ToString(TagRenderMode.Normal)
                                         , file.ToString(TagRenderMode.Normal));
            return MvcHtmlString.Create(input);
        }
        #endregion
    }
}